package cn.janyd.common.core.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Arrays;

/**
 * @author janyd
 * @since 2020/8/5 15:09
 */
public enum CommonStatusEnum implements IntArrayValuable {

    /**
     * 开启
     */
    ENABLE(1, "开启"),

    /**
     * 关闭
     */
    DISABLE(2, "关闭");

    public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(CommonStatusEnum::getValue).toArray();

    /**
     * 状态值
     */
    @EnumValue
    private final Integer value;

    /**
     * 状态名
     */
    private final String name;

    CommonStatusEnum(Integer value, String name) {
        this.value = value;
        this.name = name;
    }

    @JsonValue
    public Integer getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    /**
     * 返回枚举values
     *
     * @return int 数组
     */
    @Override
    public int[] array() {
        return ARRAYS;
    }

    @JsonCreator
    public static CommonStatusEnum parse(int value) {
        for (CommonStatusEnum status : CommonStatusEnum.values()) {
            if (status.getValue() == value) {
                return status;
            }
        }

        throw new RuntimeException("CommonStatusEnum 序列化失败");
    }
}
